package com.winit.sg.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.bean.ExecuteResult;
import com.winit.commons.util.HttpClientUtil;
import com.winit.sg.model.Sg;
import com.winit.sg.service.SgService;
import com.winit.common.sg.AnalysisUtil;
import com.winit.common.zook.ZookUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 */
@Component
@Api(value = "sgService", tags = {"sgService"}, description = "sgService")
@RestController
@RequestMapping("/api/v1/sgService")
public class SgServiceController {

    @Value("${sgUrl}")
    String sgUrl;

    @Autowired
    SgService sgService;

    @RequestMapping(value = "/initZooks", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "刷取sgService数据")
    @ResponseBody
    public void getZook(
            @ApiParam(value = "dubboUrl") @RequestParam(value = "dubboUrl", required = false) String dubboUrl
    ) throws Exception {
        sgService.SgModelInitJob(dubboUrl);
    }


    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "id查询")
    @ResponseBody
    public ExecuteResult<Map<String, Long>> getStatusCount(
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) String id
    ) {
        ExecuteResult er=new ExecuteResult();
//        er.setData(sgService.selectByPrimaryKey(id));
        return er;
    }


    @RequestMapping(value = "/getSys", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取系统数据")
    @ResponseBody
    public ExecuteResult getSys() {
        ExecuteResult er=new ExecuteResult();
        er.setData(sgService.getSys());
        return er;
    }

    @RequestMapping(value = "/getServices", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取接口基本信息")
    @ResponseBody
    public ExecuteResult<Sg> getServices(
            @ApiParam(value = "sys") @RequestParam(value = "sys", required = true) String sys
    ) {
        ExecuteResult er=new ExecuteResult();
        er.setData(sgService.getServices(sys));
        return er;
    }


//    http://172.16.3.207:680/service-gate/gate/invoke
    @RequestMapping(value = "/sgHttp", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "sgHttp")
    @ResponseBody
    public ExecuteResult sgHttp(@RequestBody JSONObject jsonObject){
        ExecuteResult er=new ExecuteResult();
        try {
            JSONObject json = JSONObject.parseObject(HttpClientUtil.doPost(sgUrl, jsonObject.toJSONString()));
            er.setData(json);
        } catch (Exception e) {
            er.setCode("1");
            er.setDesc(e.getMessage());
            return er;
        }
        return er;
    }





}
